﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Drawing;
using System.Windows.Forms;

namespace TeachMe.Application.Forms
{
    public partial class BusyForm : PseudoModalForm
    {
        delegate void UpdateStatusDelegate(string value);
        delegate void ExitDelegate();

        private const int MaxStatusLenght = 64;
        private readonly ThreadWorkerData _worker;
        private bool _canClose;

        public BusyForm(ThreadWorkerData worker)
        {
            _worker = worker;
            _worker.StatusUpdate += OnStatusUpdateNeeded;
            _worker.ThreadExit += OnThreadExit;

            InitializeComponent();
        }

        public string WaitCaption
        {
            set
            {
                txCaption.Text = value;
            }
        }

        void OnThreadExit(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new ExitDelegate(ForceClose));
            }
        }

        void OnStatusUpdateNeeded(object sender, Events.StatusUpdateEventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new UpdateStatusDelegate(UpdateStatus), e.StatusText);
            }
        }

        private void ForceClose()
        {
            _canClose = true;

            Close();
        }

        private void UpdateStatus(string value)
        {
            if (value.Length > MaxStatusLenght)
            {
                value = value.Substring(0, MaxStatusLenght - 3) + "...";
            }
            txStatus.Text = value;
        }

        private void OnFormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            e.Cancel = !_canClose;

            if (!_worker.Exiting && !_worker.Canceled)
            {
                Cancel();
            }
        }

        private void OnCancelClick(object sender, EventArgs e)
        {
            Cancel();
        }

        private void Cancel()
        {
            _worker.Cancel();
            bCancel.Enabled = false;
        }

        protected override void PseudoParentSet(PseudoModalForm pseudoParent)
        {
            Point location = pseudoParent.Location;
            location.Offset(15, 15);
            Location = location;
        }

        public void Center()
        {
            StartPosition = FormStartPosition.CenterScreen;
        }
    }
}
